#ifndef DATABASE_H
#define DATABASE_H

#include<QObject>
#include<QPointer>
#include<QtSql>

class DatabaseWorker;
class Song;
class SongData;

/*
 * Data should enter the DB in the following ways, and only in those ways
 * - Through callback from MetaDataEditor, if data was changed.
 * - Through signal/slot with Playlist, when songs are added.
 * - Perhaps in the future allow scanning of the filesystem.
 */

class Database: public QObject{
Q_OBJECT
public:
	Database(QObject *parent);
	~Database();
	
	QList<QString> getArtists();
	QList<QString> getAlbums(const QString &artist = QString());
	QList<QString> getSongs(const QString &artist = QString(), const QString &album = QString());
	
	bool addEntry(QPointer<Song> song);
	bool removeEntry(const QString &path);
	void reloadSongs(QList<QString> filepaths);
signals:
	void contentsChanged(QSet<QString> changedArtists);
private slots:
	void slotAddEntryTimeout();
	void slotWorkerFinished();
private:
	QSqlDatabase m_db;
	QList<SongData*> m_pendingSongs;
	QTimer *m_addEntryTimer;
	DatabaseWorker *m_worker;
	
	static void _safeString(const QString &string, char *out);
};

#endif
